package cn.wolfcode.wolf2w.RedisPersistence;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyStatisVoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.vo.StrategyStatisVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 利用定时器按时把redis中的vo数据持久化到mongodb数据库中
 */
@Component//组件
public class RedisDataPersinstenceOnTime {

    @Autowired
    private IStrategyStatisVoRedisService strategyStatisVoRedisService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IUserInfoService userInfoService;

    //定时,每天晚上24时会触发
    //@Scheduled(cron="0 0 24 * * ?")
    @Scheduled(cron="0/5 * * * * ?")//每五秒触发一次
    public void dowork(){

        //System.out.println("O(∩_∩)O哈哈~");

        //先从redis中把vo的数据拿出来再持久化到mongodb中去
        List<Strategy> strategies = strategyService.list();
        for (Strategy strategy : strategies) {
            StrategyStatisVo vo = strategyStatisVoRedisService.getStrategyStatisVo(strategy.getId());
            BeanUtils.copyProperties(vo, strategy);
            strategyService.update(strategy);
        }

        //用户攻略收藏列表持久化
        //从redis中取出所有用户的收藏列表
        List<UserInfo> users = userInfoService.list();
        for (UserInfo user : users) {



        }

    }
}
